myApp.controller('EmergencyInfoController', function ($scope, $rootScope, $uibModal, $stateParams, $http, ngDialog) {
    $scope.info = {};
    $scope.id = $stateParams.id;
    //项目信息
    $scope.info.planInfo = {};
    $scope.info.index = 0;
    $scope.info.applyInfo = [];

    $scope.total = 0;
    $scope.page = {size: 20, index: 1};

    $scope.info.checked = false;
    //项目内存在状态不为TO_INTEGRATION的工程时，项目不能转测集成
    $scope.info.can_Integration = false;

    $scope.info.modifiAble = true;
    $scope.info.integrationInfo = null;

    //跳转工程构建页
    $scope.link = function (i) {
        setStorage('project_go_back', '1');
        window.location.href = "#/menu/project/" + i.project_name;
    };

    $scope.getProjectEditionList = function (object) {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/project/getProjectEditionList?token_id=' + getTokenId() + '&project_name=' + object.project_name, function (data) {
            object.editionList = data.data;
            angular.forEach(object.editionList, function (edition) {
                if (edition.editionNum == 'default') {
                    object.build_status = edition.showNum.split('(')[1].split(')')[0];
                }
            });
        });
    };

    $scope.getPlanInfo = function () {
        $scope.info.integrationInfo = null;
        doBeetleGet($http, $uibModal, beetle_plan_url + '/project_plan/getPlanList?withIntPro=true&hasProList=true&isEmergency=0&token_id=' + getTokenId() + '&plan_id=' + $scope.id + '&isShowOnline=true', function (data) {
            $scope.info.planInfo = data.data[0];
            $scope.total = $scope.info.planInfo.projects.length;
            angular.forEach($scope.info.planInfo.projects, function (pro) {
                var branchTip = '<table class="table table-bordered">';
                branchTip += '<tr><td>当前分支:</td><td>' + pro.project_name + '</td></tr>';
                if (pro.svn_from != null) {
                    branchTip += '<tr><td>来源:</td><td>' + pro.svn_from + '</td></tr>';
                }
                if (pro.from_version != null) {
                    branchTip += '<tr><td>来源版本号:</td><td>' + pro.from_version + '</td></tr>';
                }
                if(pro.from_svn_version != null){
                    branchTip += '<tr><td>来源svn号:</td><td>' + pro.from_svn_version + '</td></tr>';
                }
                branchTip += '</table>';

                pro.branchTip = branchTip;
                $scope.getProjectEditionList(pro)
            });
            doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/getIntegrationByPlan?token_id=' + getTokenId() + '&plan_id=' + $scope.id, function (data) {
                if (data.success) {
                    $scope.info.integrationInfo = data.data[0];
                }
            });
        })
    };

    $scope.title = function (index) {
        if (index == 0) {
            $scope.getPlanInfo();
        }
        $scope.info.index = index;
        if (index == 1) {
            $scope.apply_record();
        }
        if (index == 2) {
            var count = 0;
            angular.forEach($scope.info.planInfo.projects, function (pro) {
                if (pro.package_type != 'maven_jar' && pro.project_status != 'TO_INTEGRATION') {
                    count = 1;
                    return
                }
            });
            if (count == 0) {
                $scope.info.can_Integration = true;
            }
        }
    };
    $scope.title(0);

    $scope.del = function (i) {
        openDialog(ngDialog, $scope, "删除不可恢复，请确认！", true, function (val) {
            if (!val) {
                return;
            }
            openDialog(ngDialog, $scope, "是否要删除工程的svn目录?", true, function (val) {
                doBeetleGet($http, $uibModal, beetle_plan_url + '/project_plan/delPlanProject?token_id=' + getTokenId() + '&trunk_name=' + i.trunk_name + '&plan_id=' + $scope.id + '&is_del_svn=' + val, function (data) {
                    if (data.success) {
                        $scope.getPlanInfo();
                    } else {
                        openDialogAlert(ngDialog, $scope, data.msg);
                    }
                })
            });
        })
    };

    $scope.changePlanPro = function (i) {
        $rootScope.changePlanProjectTemplate($scope.info.planInfo.plan_id, i.trunk_name, function () {
            $scope.getPlanInfo();
        }, function () {
            $scope.getPlanInfo();
        })
    };

    $scope.apply_record = function () {
        LoadModal($uibModal);
        $http.get(beetle_plan_url + '/project_plan/getUcloudApplyInfo?token_id=' + getTokenId() + '&plan_id=' + $scope.id).success(function (data) {
            closeModal();
            $scope.info.applyInfo = data.data;
        }).error(function (data) {
            alert(data.message);
            closeModal();
        })
    };

    $scope.toAgent = function (apply) {
        window.location.href = "#/menu/newucList/" + apply.agent_ip;
    };

    $scope.project_edit = function (object) {
        $rootScope.editionToTestTemplate(object.project_name, "TEST", function () {
            $scope.getPlanInfo();
        });
    };

    $scope.updateToIntegrationEdition = function (object) {
        $rootScope.editionToTestTemplate(object.project_name, "INTEGRATION", function () {
            $scope.getPlanInfo();
        });
    };

    $scope.add = function () {
        $rootScope.addPlanProjectTemplate($scope.info.planInfo.plan_id, function () {
            $scope.getPlanInfo();
        }, function () {
            $scope.getPlanInfo();
        })
    };

    $scope.doIntegration = function () {
        // var flag = true;
        // var name = '';
        // angular.forEach($scope.info.planInfo.projects, function (pro) {
        //     if(pro.project_status != 'PRO_MERGED' && ((pro.isOnBeetle == '0' && pro.package_type != 'maven_jar') || (pro.package_type == 'ad'))){
        //         if (pro.to_integration_edition == null || pro.to_integration_edition == '') {
        //             flag = false;
        //             name = name + pro.trunk_name + ',';
        //         }
        //     }
        // });
        // if (!flag) {
        //     openDialogAlert(ngDialog, $scope, "工程" + name.substring(0, name.length - 1) + "申请集成版本不能为空!");
        //     return
        // }
        doBeetleGet($http, $uibModal, beetle_plan_url + '/project_plan/toIntegrationPlan?plan_id=' + $scope.id + '&token_id=' + getTokenId(), function (data) {
            if (data.success) {
                //成功后自动创建集成计划
                doBeetleGet($http, $uibModal, beetle_plan_url + '/integration/createEmergencyIntegrationPlan?token_id=' + getTokenId() + '&emergency_plan_id=' + $scope.id, function (data) {
                    if (data.success) {
                        window.location.href = "#/menu/integration/"
                    } else {
                        openDialogAlert(ngDialog, $scope, data.msg);
                    }
                })
            } else {
                openDialogAlert(ngDialog, $scope, data.msg);
            }
        });
    };
});